package org.example.JDBC;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:Lenovo
 * Date:2022-04-09
 * Time:14:37
 */
public class Update {
    public static void main(String[] args) throws SQLException {
        //1.创建数据源
        DataSource dataSource = new MysqlDataSource();
        //设置数据库所在的地址
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/zyp?characterEncoding=utf8&useSSL=false");
        //设置登录数据库的用户名
        ((MysqlDataSource) dataSource).setUser("root");
        //设置登录数据库的密码
        ((MysqlDataSource) dataSource).setPassword("123456");
        //2.建立链接
        Connection connection = dataSource.getConnection();
        //3.用户输入修改信息
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要修改的id:");
        int opl = scanner.nextInt();
        System.out.println("请输入你要修改后的名字:");
        String str1 = scanner.next();
        //4.拼装sql语句
        String sql = "update book set name = ? where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,str1);
        statement.setInt(2,opl);
        System.out.println("statement = " + statement);
        //5.执行sql语句
        int ret = statement.executeUpdate();
        System.out.println("ret = " + ret);
        //6.回收释放资源
        scanner.close();
        statement.close();
        connection.close();
    }
}
